from socket import *
from Tkinter import *
from select import *

class ChatBox:
	def __init__(self,geometry = '800x600'):
		self.root = Tk()
		self.root.geometry(geometry)

		self.full = PanedWindow(self.root)
		self.full.pack(fill=BOTH, expand=1)

		self.left = PanedWindow(self.full, orient=VERTICAL)
		self.full.add(self.left)

		self.right = PanedWindow(self.full, orient=VERTICAL)
		self.full.add(self.right)

		self.text = StringVar()
		self.display = Label(self.left,justify = LEFT, width = 50,height = 30,background = 'white', textvariable = self.text)
		self.left.add(self.display)
		self.display.pack()

		self.entry = Entry (self.left,width = 50)
		self.left.add(self.entry)
		self.entry.pack()

		self.title = Label(self.right, text = 'Online')
		self.right.add(self.title)
		self.title.pack()

		self.names = StringVar()
		self.members = Label(self.right,justify = LEFT, width = 50,height = 30,background = 'white', textvariable = self.names)
		self.right.add(self.members)
		self.members.pack()

		self.connect()

		self.entry.bind ( '<Return>', self.send)
		while True:
			r, w, e = select([self.socket], [], [], 1.0)
			if len(r) != 0:
				self.text.set(self.text.get()+r[0].recv(1000))
			self.root.update()


	def connect(self,host = '192.168.0.6',port = 6010):
		while True:
			try:
				print 'Attempting to connect to '+host+' at port '+str(port)+'.'
				# Make an internet socket, TCP connection mode
				self.socket = socket(AF_INET, SOCK_STREAM)

				# Connect to localhost on port 9999
				self.socket.connect((host,port))
				return
			except:
				print 'Connection Failed.\n'

	def send(self,event):
		#self.text.set(self.entry.get())
		self.socket.send(self.entry.get())
		self.entry.delete(0, END)
a=ChatBox()

